package com.knowledge.business.service.impl;

import com.knowledge.business.service.IKnowledgeDocumentSegmentService;
import com.knowledge.business.util.SiliconFlowEmbeddingUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class VectorSearchService {

    @Autowired
    private VectorStorageService vectorStorageService;

    @Autowired
    private IKnowledgeDocumentSegmentService documentSegmentService;

    @Autowired
    private SiliconFlowEmbeddingUtil siliconFlowEmbeddingUtil;

    /**
     * 根据查询内容搜索相似的文档分段
     *
     * @param query 查询内容
     * @param topK  返回最相似的K个结果
     * @return 相似的文档分段列表
     */
    public List<VectorStorageService.SearchResult> search(String query, int topK) {
        try {
            // 生成查询向量
            List<Float> queryVector = siliconFlowEmbeddingUtil.generateEmbedding(query);

            // 在Milvus中搜索相似向量
            return vectorStorageService.searchSimilarVectors(queryVector, topK);
        } catch (Exception e) {
            throw new RuntimeException("向量搜索失败: " + e.getMessage(), e);
        }
    }
}
